package com.Login.Service;

import com.Login.dao.LoignDao;
import com.Pojo.User;
import com.Utils.RedisUtil;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by XingYang on 2019/3/1.
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    LoignDao loignDao;

    @Override
    public List<User> getAllUser() {


        return loignDao.getAllUser();
    }

    @Override
    public User getUserById(String uid) {
        return loignDao.getUserById(uid);
    }
    /**
     * 从role表中获取角色数据
     */
    @Override
    public PageInfo getRolesInfByUid(String uid,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(loignDao.getRolesInfByUid(uid));
        return pageInfo;
    }

    @Override
    public User getUser(String username) {
        return loignDao.getUserByUsername(username);
    }

    @Override
    public List<String> getRolesByUid(String uid) {
        return loignDao.getRolesByUid(uid);
    }

    @Override
    public List<String> getPowersByUid(String uid) {
        return loignDao.getPowersByUid(uid);
    }

    @Override
    public List<String> getPowersByRid(String rid) {
        return loignDao.getPowersByRid(rid);
    }

    @Override
    public Map getRoleInfByRid(String rid) {
        return loignDao.getRoleInfByRid(rid);
    }

    @Override
    public List<Map> getAllPowers() {
        return loignDao.getAllPowers();
    }

    @Override
    public List<Map<String,String>> getPermissionConfig() {
        return loignDao.getPermissionConfig();
    }


    @Override
    public void addUser(User user) {
        loignDao.addUser(user);
    }
    RedisUtil redisUtil = new RedisUtil();

    @Override
    public boolean refreshUser(String username){
        try{
            User user  = loignDao.getUserByUsername(username);
            List<String> roles = new ArrayList<>();
            List<String> powers = new ArrayList<>();
            LinkedHashSet<String> powerSet = new LinkedHashSet<>();
            roles = loignDao.getRolesByUid(user.getId());
            powerSet.addAll(loignDao.getPowersByUid(user.getId()));
            for(String role:roles){
                powerSet.addAll(loignDao.getPowersByRid(role));
            }
            user.setRoles(roles);
            powers.addAll(powerSet);
            user.setPowers(powers);
            System.out.println(user.toString());
            Jedis jedis = redisUtil.getClient();
            jedis.del(username.getBytes());
            jedis.lpush(username.getBytes(),redisUtil.serializeObj(user));
            roles.clear();
            powers.clear();
            powerSet.clear();
        }catch (Exception e){
            return false;
        }
        return true;

    }
//创建新角色
    @Override
    public void addRole(Map map) {
        String[] powers = ((String)map.get("powers")).split(",");
        map.remove("powers");
        loignDao.addRole(map);
        String roleid =  map.get("role_id").toString();
        Map<String,String> po = new HashMap();
        po.put("roleid",roleid);
        for (String power:powers){
            po.put("powerid",power);
            loignDao.roleBindPower(po);
        }

    }

    @Override
    public boolean modifyRole(Map map) {

            String[] powers = ((String) map.get("powers")).split(",");
            map.remove("powers");
            int flag = loignDao.modifyRole(map);
            String roleid = map.get("roleId").toString();
            //删除角色与权限绑定关系
            loignDao.roleUnBindPower(roleid);
            //重新绑定角色权限
            Map<String, String> po = new HashMap();
            po.put("roleid", roleid);
            for (String power : powers) {
                if(StringUtils.isEmpty(power)){
                 return true;
                }

                po.put("powerid", power);
                loignDao.roleBindPower(po);
            }
        if(flag<0){
            return false;
        }

        return true;
    }

    @Override
    public int delRole(String rid) {
        return loignDao.delRoleByRId(rid);
    }

    @Override
    public int disableRoleByRId(String Rid) {
        return loignDao.disableRoleByRId(Rid);
    }

    @Override
    public int enableRoleByRId(String Rid) {
        return loignDao.enableRoleByRId(Rid);
    }

    @Override
    public List<Map> queryManagerByMainId(String mainaccount) {
        return loignDao.queryManagerByMainId(mainaccount);
    }

    @Override
    public int delRedisKey(String key) {
        Jedis jedis = redisUtil.getClient();
        int i = jedis.del(key).intValue();
        jedis.close();
        return i;
    }
}
